package com.phshopping.cash.api.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.pay.service.gzylcash.GZYLCashService;
import com.ph.shopping.facade.pay.utils.gzyl.SystemRetCode;
import com.phshopping.utils.SerializableUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @项目：phshopping-facade-pay
 * @描述：
 * @作者： 张霞
 * @创建时间： 14:02 2017/4/10
 * @Copyright @2017 by zhangxia
 */
@Controller
@RequestMapping("api/cash")
public class GZYLDrawcashCallBackController {
    // 创建公共日志
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Reference(version="1.0.0")
    private GZYLCashService gzylCashService;

    @RequestMapping(value = "/cashAsyncCallBack" ,method = RequestMethod.POST)
    @ResponseBody
    public String cashAsyncCallBack(HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("贵州银联代付回调开始------------------------------------------------");
        // begin 接收post请求报文
        String call = "error";
        BufferedReader buff = request.getReader();
        StringBuilder builder; // 密文
        try {
            builder = new StringBuilder(); // 密文
            while (true) {
                String temp = buff.readLine();
                logger.info("密文:"+temp);
                if (null == temp)
                    break;
                builder.append(temp);
                if (100000 <= builder.length()) {
                    //errorRsp(response, "02000", "网关数据请求错误！");
                    return "error";
                }
            }
            logger.info("####################");
            String encReq = builder.toString();
            logger.info("###########"+encReq);

//            String encReq = "{\"sign\":\"hXEJxmIzEQo3x8Br0Y0UDNc/IQ+TdK+tVkSfMpDmuhK1uzZHGdamy0P1vPM/V0mFx7BygWhSn9m+/RPujvXz8uAZfHoRXo/KCWwNyvIp0EFR5RMG3fLbUxQJBuFcSk8DHcfDWwmvwnZXbmvaYQgPODdhBg6BwYr8sHwRlvARr+RqsjzIeRDX0Kg4lCmprhs9W/H7aCYgEpbKRLAcYRjzH7DeyFQuxGedUXs3388tt/pfgJlyi1Tfg4sLFcbCsQYv8jtSJhP7UAUqgxfQrsE4QS466akr0e9koZcsJYbrTa6AddROP9a9kU2DQshywfNn3sy250nILwcg0jcpEhfTEA==\",\"custOrderId\":\"PHTX04201743804113942451\",\"transStatusDesc\":\"成功-00\",\"transStatus\":\"SUCCESS\",\"orderId\":\"2017041201082624612\"}";
            encReq = SerializableUtils.serialize(encReq);
            Result result = gzylCashService.validation(encReq);
            call = result.getMessage();
            logger.info("*********"+call);
        } catch (Exception e) {
            logger.info("aaa回调方法处理中,发生异常");
            e.printStackTrace();
            buff.close();
        } finally {
            buff.close();
        }
        if (SystemRetCode.SUCCESS.toString().equals(call)) {
            response.getOutputStream().println("success");
            logger.info("回调方法处理完毕,返回的字符串"+call+",通知贵州银联收到回调");
            return "success";
        }else {
            return call;
        }
    }

}
